package com.aponisipsis.gl3d.render;

import static org.lwjgl.opengl.GL11.GL_LINES;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glVertex3f;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL21;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL32;
import org.lwjgl.opengl.GL33;
import org.lwjgl.opengl.GL42;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import com.aponisipsis.gl3d.data.ResourceManager;
import com.aponisipsis.gl3d.geometry.Material;
import com.aponisipsis.gl3d.geometry.Mesh2D;
import com.aponisipsis.gl3d.geometry.MeshLayerComparator;
import com.aponisipsis.gl3d.geometry.Model;


public class RenderManager {
	private int NUM_LAYERS = 20;
	int width, height;
	boolean fullScreen;
	
	List<List<Mesh2D>> meshes;
	float zoom;
	float x,y;
	
//	CollisionConfiguration cc = new DefaultCollisionConfiguration();
//	CollisionDispatcher dispatcher = new CollisionDispatcher(cc);
	
	public RenderManager(int width, int height, boolean fullScreen) {
		
		this.width = width;
		this.height = height;
		this.fullScreen = fullScreen;

		meshes = new ArrayList<List<Mesh2D>>();
		
		for(int c=0;c<NUM_LAYERS;c++){
			meshes.add(new ArrayList<Mesh2D>());
		}
		
		zoom = 1;
	}

	public void init() throws LWJGLException {
		Display.setDisplayMode(new DisplayMode(width, height));
		Display.setFullscreen(fullScreen);
		Display.create();
		
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		
		GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		
		GL11.glEnable(GL11.GL_NORMALIZE);

	}

	public void render() {
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GLU.gluOrtho2D(-Display.getWidth()/2,Display.getWidth()/2,-Display.getHeight()/2,Display.getHeight()/2);

		GL11.glScalef(zoom, zoom, 1);
		GL11.glTranslatef(x, y, 0);
		
		for(List<Mesh2D> layer:meshes){
			for(Mesh2D mesh:layer){
				mesh.render();
			}
		}
	}

	public void addMesh(Mesh2D mesh){
		this.meshes.get(mesh.getLayer()).add(mesh);
	}

	public static void main(String[] args) throws Exception{
		
		RenderManager rm = new RenderManager(800,600, false);

		rm.init();						
		
		ResourceManager.loadScene("d:/3d/testscene.json");
		
		
		
		for(Mesh2D mesh:ResourceManager.getAllMeshes()){
			rm.addMesh(mesh);
		}

		while(!Display.isCloseRequested()){
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			rm.handleInput();
			rm.render();
			Display.sync(60);
			Display.update();
		}
	}
	
	

	public float getZoom() {
		return zoom;
	}

	public void setZoom(float zoom) {
		this.zoom = zoom;
	}

	public void handleInput() {
		// Keyboard.poll();

		while (Keyboard.next() || Mouse.next()) {

			if (Keyboard.getEventKey() == Keyboard.KEY_A) {
				GL11.glEnable(GL11.GL_BLEND);
				//GL11.glBlendFunc(GL11.GL_SRC_ALPHA_SATURATE, GL11.GL_ONE );
				GL11.glEnable(GL11.GL_BLEND );;
				GL11.glHint(GL11.GL_POLYGON_SMOOTH_HINT,GL11.GL_NICEST);
				GL11.glEnable(GL11.GL_POLYGON_SMOOTH);

			}
			
			if (Keyboard.getEventKey() == Keyboard.KEY_1) {

			}
			if (Keyboard.getEventKey() == Keyboard.KEY_2) {
//				GL11.glHint(GL11.GL_LINE_SMOOTH_HINT,GL11.GL_FASTEST);
			}
			if (Keyboard.getEventKey() == Keyboard.KEY_3) {
	//			GL11.glHint(GL11.GL_LINE_SMOOTH_HINT,GL11.GL_FASTEST);
			}

			if (Mouse.isButtonDown(0)) {
				x += Mouse.getDX()/zoom;
				y += Mouse.getDY()/zoom;
			}
			
			if(Mouse.getEventDWheel()!=0){
				zoom+=Mouse.getEventDWheel()/100;
				System.out.println(zoom);
			}
		}
	}
}
